import path from 'path';

import { runHvigorAsync } from '../../common/common';
import { addOhPackageDps, commonFileOperation } from '../../plugins/common-fun';
import {
  AbilityType,
  APIType,
  APIVersion, CaseLevel,
  LanguageType, ModuleTemplateType,
  RuntimeOSType,
  SDKType
} from '../../plugins/project-builder/models/constants';
import { TestCaseBuilder } from '../../plugins/task-builder/task-case-builder';
import { ProjectBuildOption, TestCaseFun } from '../../plugins/task-builder/test-case';
import { TestTaskOptionBuilder } from '../../plugins/task-builder/test-task-option';


const projectBuilderOption: ProjectBuildOption = {
  template: {
    sdk: SDKType.Single,
    runtimeOS: RuntimeOSType.HarmonyOS,

    // 当前都是5.0 api已经不用管
    apiVersion: APIVersion.API10,
    apiType: APIType.FA,
    language: LanguageType.Js,
    abilityType: AbilityType.Application,
    entryTemplateType: ModuleTemplateType.LiteEmptyAbility
  },
  modulesOption: [ModuleTemplateType.JsStaticLibrary]
};

// 1.lite工程新建js的har模块，entry依赖js的har模块运行模拟器失败
const testfun: TestCaseFun = async (project, taskConfig, record) => {
  const entryPath = project.getModule('entry').moduleRootPath();

  // 1.添加依赖
  await addOhPackageDps(entryPath, {
    'jsStaticLibrary': '../jsStaticLibrary'
  });

  // 2. 在entry模块的index.js文件中进行jsStaticLibrary方法的引用
  await commonFileOperation(path.resolve(entryPath, 'src/main/js/MainAbility/pages/index/index.js'), async (buffer) => {
    const content = buffer.toString();
    return `import { func } from 'jsStaticLibrary'\n\nfunc()\n${content}`;
  });

  // 3.requiredDeviceType=liteWearable构建并执行成功
  const commands: string[] = 'assembleHap -p buildMode=release -p requiredDeviceType=liteWearable'.split(' ');
  const result = await runHvigorAsync(project.projectRootPath(), commands);
  record.addStep('初始构建liteWearable瘦设备工程', result);
  expect(result.stdout).toContain('BUILD SUCCESSFUL');
};

TestCaseBuilder.builder()
  .caseName('DTS2024060423216')
  .description('lite工程，entry依赖js的har模块，' +
    '运行模拟器失败The type of the target device does not match the deviceType configured by the selected module')
  .projectBuilderOption(projectBuilderOption)
  .taskConfig(TestTaskOptionBuilder.builder().caseLevel(CaseLevel.DTS).build())
  .testFun(testfun)
  .load();